# _*_ coding : utf-8 _*_
# @Time: 2024/7/16 9:37
# @Author : 石文帅	
# @File : test
# @Project : python-interface-automation

import unittest
import paramunittest
import requests
from BeautifulReport import BeautifulReport
import time
import json
from common import BasePage
from common.ExcelData import ExcelData
from common.LoginInterface import LoginInterface

# print('登录并把token设置到ini')
# login = LoginInterface(BasePage.get_db_data('api', 'url'))
# BladeAuth = login.get_headers(BasePage.get_db_data('api', 'user'), BasePage.get_db_data('api', 'password'))
# BasePage.set_db_data('api', 'Blade-Auth', BladeAuth)

listData = ExcelData('test.xls').get_values_to_dict()


@paramunittest.parametrized(*listData)
class test_Api(unittest.TestCase):
    def setParameters(self, Case_number, modular, Case_title, path, method, Request_parameters, Return_value):
        """这里注意了，user, psw, result三个参数和前面定义的字典一一对应"""
        self.Case_number = Case_number
        self.modular = modular
        self.Case_title = Case_title
        self.path = path
        self.method = method
        self.Request_parameters = Request_parameters
        self.Return_value = Return_value

    def test_api(self):
        '''接口测试'''

        method = self.method
        print("method = " + method)
        url = BasePage.get_db_data('api', 'url') + self.path
        print("url = " + url)

        #字符串转字典 再转json
        payload = json.dumps(json.loads(self.Request_parameters))
        print("json.loads = " + payload)
        headers = {
            "Blade-Auth": BasePage.get_db_data('api', 'blade-auth'),
            "Content-Type": "application/json;charset=UTF-8"
        }

        response = BasePage.send_method(url=url, method=method, data=payload, headers=headers)

        #实际结果
        print(f'实际结果: {response["body"]}')

        #预计结果
        print(f"预计结果: {self.Return_value}")

        dict_value = json.loads(self.Return_value)

        msg = dict_value['msg']
        code = dict_value['code']
        success = dict_value['success']

        self.assertEqual(code, response["body"]['code'])
        self.assertEqual(msg, response["body"]['msg'])
        self.assertEqual(success, response["body"]['success'])


if __name__ == '__main__':
    suite = unittest.defaultTestLoader.discover('.', pattern='test*.py')
    br = BeautifulReport(suite)
    br.report(description='测试描述', filename='测试报告.html')
